package com.ruoyi.ahs.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ahs.cache.AhsCache;
import com.ruoyi.ahs.util.RushTaskDto;
import com.ruoyi.system.domain.RushTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RushConfig;
import com.ruoyi.system.service.IRushConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 爱回收配置Controller
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@Slf4j
@RestController
@RequestMapping("/system/ahsconfig")
public class RushConfigController extends BaseController {
    @Autowired
    private IRushConfigService rushConfigService;

    /**
     * 查询爱回收配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:ahsconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(RushConfig rushConfig) {
        startPage();
        List<RushConfig> list = rushConfigService.selectRushConfigList(rushConfig);
        return getDataTable(list);
    }

    /**
     * 导出爱回收配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:ahsconfig:export')")
    @Log(title = "爱回收配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RushConfig rushConfig) {
        List<RushConfig> list = rushConfigService.selectRushConfigList(rushConfig);
        ExcelUtil<RushConfig> util = new ExcelUtil<RushConfig>(RushConfig.class);
        util.exportExcel(response, list, "爱回收配置数据");
    }

    /**
     * 获取爱回收配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ahsconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(rushConfigService.selectRushConfigById(id));
    }

    /**
     * 新增爱回收配置
     */
    @PreAuthorize("@ss.hasPermi('system:ahsconfig:add')")
    @Log(title = "爱回收配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RushConfig rushConfig) {
        return toAjax(rushConfigService.insertRushConfig(rushConfig));
    }

    /**
     * 修改爱回收配置
     */
    @PreAuthorize("@ss.hasPermi('system:ahsconfig:edit')")
    @Log(title = "爱回收配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RushConfig rushConfig) {
        if (1L == rushConfig.getId()) {
            AhsCache.setCurrentConfig(rushConfig);
            log.info("爱回收配置已更新");
        }
        return toAjax(rushConfigService.updateRushConfig(rushConfig));
    }

    /**
     * 删除爱回收配置
     */
    @PreAuthorize("@ss.hasPermi('system:ahsconfig:remove')")
    @Log(title = "爱回收配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            if (id == 1L) {
                throw new RuntimeException("不允许的的操作");
            }
        }
        return toAjax(rushConfigService.deleteRushConfigByIds(ids));
    }

    @PostConstruct
    public void init() {
        log.info("加载爱回收配置中......");
        RushConfig rushConfig = rushConfigService.selectRushConfigById(1L);
        AhsCache.setCurrentConfig(rushConfig);
        log.info("加载爱回收配置成功：{}", rushConfig.toString());
    }

}
